package com.small.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;


/**
 * @program: small
 * @description:
 * @author: Mr.Xie
 * @create: 2019-04-25 09:49
 */
@Slf4j
public class PropertiesUtil {

    private static Properties properties;

    static {
        String fileName = "mmall.properties";
        properties= new Properties();
        try {
            properties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
            log.info("PropertiesUtil读取文件");
        } catch (IOException e) {
            log.error("配置文件读取异常",e);
        }
    }

    public static String getProperty(String key){
        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return null;
        }
        return value.trim();
    }
    public static Integer getIntegerProperty(String key){
        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return null;
        }
        return Integer.parseInt(value);
    }
    public static Boolean getBooleanProperty(String key){
        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return null;
        }
        return Boolean.parseBoolean(value);
    }
    public static String getProperty(String key,String defaultValue){
        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return defaultValue;
        }
        return value.trim();
    }

    public static Integer getIntegerProperty(String key,Integer defaultValue){
        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return defaultValue;
        }
        return Integer.parseInt(value);
    }

    public static Boolean getBooleanProperty(String key,boolean defaultValue){
        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return defaultValue;
        }
        return Boolean.parseBoolean(value);
    }

}
